package com.guocheng.modules.login.service;

import com.guocheng.base.BaseService;
import com.guocheng.modules.login.mapper.UserDao;
import com.guocheng.modules.login.model.po.Role;
import com.guocheng.modules.login.model.po.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Service
public class UserService extends BaseService<UserDao, User> {

    /**
     * 根据账户查询用户信息
     * @param name 账户
     * @return 用户信息
     */
    public User selectByUsername(String name){
        return baseDao.selectByUsername(name);
    }

    /**
     * 根据用户id查询角色id
     * @param id    用户id
     * @return  角色id
     */
    public List<Integer> selectRoleById(Integer id) {
        return baseDao.selectRoleById(id);
    }

    /**
     * 修改用户角色信息
     * @param id    用户id
     * @param ids   角色ids
     * @return
     */
    @Transactional
    public boolean insertUserRole(Integer id, List<Integer> ids) {
        int i = baseDao.deleteUserRoleByUserId(id);
        if(ids==null||ids.size()<=0) return true;
        return baseDao.insertUserRole(id,ids)>0;
    }

    /**
     * 删除用户角色中间表
     * @param ids
     */
    @Transactional
    public boolean deleteByIds(List<Integer> ids ) {
        int i=baseDao.deleteBatchIds(ids);
        int b=baseDao.deleteByUserIds(ids);
        return true;
    }
}
